package myJs.user.workflow.featureSelection.boruta

import myJs.myPkg.reactHookForm._
import shared.pojo.WorkflowMission._

import myJs.tool.Tool._
import com.softwaremill.quicklens._
import myJs.Implicits._

import scala.scalajs.js
import myJs.Implicits._
import myJs.cps._
import myJs.tool.Tool

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._

/** Created by yz on 3/11/2020
  */
object BorutaCp {

  case class Props(
      curI: Int,
      data: EachBorutaData,
      dataChange: EachBorutaData => SyncIO[Unit],
      dataModifyF: (EachBorutaData=>EachBorutaData) => SyncIO[Unit],
      fsData: FsData
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val curTool = Boruta
    val namePrefix = s"boruta.${props.curI}"

    object FTool {

      def scaleMethodChange = (v: String) => {
        val newData = props.data.modify(_.scaleMethod).setTo(v)
        props.dataChange(newData)
      }

      def groupsModifyF = (v: List[String]) => {
        val newData = modifyLens[EachBorutaData](_.groups).setTo(v)
        props.dataModifyF(newData)
      }

      def maxRunChange = (v: String) => {
        val newData = props.data.modify(_.maxRun).setTo(v)
        props.dataChange(newData)
      }

      def pValueChange = (v: String) => {
        val newData = props.data.modify(_.pValue).setTo(v)
        props.dataChange(newData)
      }

    }
    val formState = ReactHookForm.useFormState(options =
      UseFormStateOptions(
        name = namePrefix
      )
    )

    useEffect(
      SyncIO{
        Tool.refreshNodeColor(props.data.nodeId, formState, namePrefix)
      },
      List(formState)
    )
    val dataMatrixGroups = props.fsData.dataMatrix.groups
    useEffect(
      {
        FTool.groupsModifyF(dataMatrixGroups)
      },
      List(dataMatrixGroups)
    )

    val groupMap = dataMatrixGroups.optionMap

    div(
      RSelectMulCp.Props(
        value = props.data.groups,
        label = messages("groups"),
        name = s"${namePrefix}.groups",
        optionMap = groupMap,
        onChange = FTool.groupsModifyF
      ).render,
      SelectCp.Props(
        value = props.data.scaleMethod,
        label = messages("scaleMethod"),
        name = s"${namePrefix}.scaleMethod",
        optionMap = scaleMethodMap,
        onChange = FTool.scaleMethodChange
      ).render,
      InputCp.Props(
        value = props.data.maxRun,
        show = true,
        label = messages("maxNumberOfRuns"),
        name = s"${namePrefix}.maxRun",
        onChange = FTool.maxRunChange
      ).render,
      InputCp.Props(
        value = props.data.pValue,
        show = true,
        label = messages("pValueCutoff"),
        name = s"${namePrefix}.pValue",
        onChange = FTool.pValueChange
      ).render
    )

  }

}
